﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using CPPProject.CppModel;
using CPPProject.CppServices.Dto;
using Microsoft.EntityFrameworkCore;
using OplugAbpProject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace CPPProject.CppServices
{
    public class T_Biz_CustomerAppService : AsyncCrudAppService<T_Biz_Customer, T_Biz_CustomerDto, Guid, PagedResultRequestDto, UpdateT_Biz_CustomerDto, UpdateT_Biz_CustomerDto>, IT_Biz_CustomerAPPService
    {
        private readonly IObjectMapper _objectMapper;
        public T_Biz_CustomerAppService(IRepository<T_Biz_Customer, Guid> repository, IObjectMapper objectMapper) : base(repository)
        {
            _objectMapper = objectMapper;
        }
        #region 客户管理
        /// <summary>
        /// 客户管理
        /// </summary>
        /// <param name="nCompanyID">公司ID</param>
        /// <param name="cCusCode">客户编码</param>
        /// <param name="cName">客户名称</param>
        /// <param name="skipCount"></param>
        /// <param name="maxResultCount"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<T_Biz_CustomerDto>> GetList(string nCompanyID, string cCusCode = "",string cName="", int skipCount = 0, int maxResultCount = 10)
        {
            PagedResultDto<T_Biz_CustomerDto> resultDto = new PagedResultDto<T_Biz_CustomerDto>();
            List<T_Biz_Customer> lists = new List<T_Biz_Customer>();
            Expression<Func<T_Biz_Customer, bool>> expr = o => o.NCompanyID == nCompanyID;
            if (!string.IsNullOrEmpty(cCusCode))//客户编码
            {
                expr = expr.ExpressionAnd(o => o.CCusCode == cCusCode);
            }
            if (!string.IsNullOrEmpty(cName))//客户名称
            {
                expr = expr.ExpressionAnd(o => o.CName == cName);
            }
            lists = await Repository.GetAll().Where(expr).ToListAsync();
            var dtos = lists.Skip(skipCount).Take(maxResultCount).ToList();
            resultDto.Items = _objectMapper.Map<List<T_Biz_CustomerDto>>(dtos);
            resultDto.TotalCount = lists.Count;
            return resultDto;
        }
        #endregion

        #region 查询全部
        /// <summary>
        /// 查询全部
        /// </summary>
        /// <param name="nCustomerID">客户ID</param>
        /// <returns></returns>
        public async Task<T_Biz_CustomerDto> GetList2(string cCusCode)
        {
           var  lists = await Repository.GetAllListAsync(o=>o.CCusCode== cCusCode);
            return _objectMapper.Map<T_Biz_CustomerDto>(lists);
        }
        #endregion


    }
}
